import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const message 			= $Lazy.file(()=>import('@antd/message'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		// 批量项目作废
		formData:[
			{ label: '开始条码', 	name:'start_code', 	half:true },
			{ label: '结束条码', 	name:'end_code', 	half:true,	
				btn:{ label:'生成', 	 onClick:value=>{this.generateCode()}}
			},
			{ label: '条码号', 		name:'spec_codes', 	full:true, 	type:'textarea',	required:true },
			{ label: '项目名称', 	name:'kind_id', 	full:true, 		type:'select'}, 
			{ label: '姓名', 		name:'name', 		full:true,		p:' ',			disabled:true },
			{ label: '用户名', 		name:'account', 	half:true,							required:true,	onBlur:(value)=>{this.getName(value)} },
			{ label: '密码', 		name:'password', 	half:true, 		type:'password',	required:true,	onBlur:(value)=>{this.getName(value)} },
		]
	}
	
	submit = {}
	spec_code = []

	// 弹窗获取下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 项目名称
			$fn.setRow( formData, 'kind_id',  await f.getCache({ api:'nature_project' }))
			
			this.setState({ formData })
		})
	}
	// 根据账号获取姓名
	getName = (value)=>{
		if(value){
			const formValue = this.formRef.getValue(['account','password'])
			$http.submit(null, 'employee/getuserrealname', {param:{phone:formValue.account,password:formValue.password},loading:false }).then((data)=> {
				if(!$fn.hasArray(data)){
					if(data.length === 0){
						this.formRef.setValue({name:''})
					}else{
						this.formRef.setValue({name:data.real_name})
					}
				}
			})
		}
	}
	
	open = (rows)=> {
		const userData = $fn.getUser()
		const { empl_name, real_name } = userData
		const m = $fn.longSave('remember')
		if($fn.hasObject(m)){
			for(var i in m){ m[i] = Encrypt.decode(m[i]) }
		}
		// 传入外部的条码
		let spec_code = rows.map(v => v.spec_code)	/* 条码去重 2021-8-10  --马婷 */
		spec_code = Array.from(new Set(spec_code))
		this.formRef.setValue({account: empl_name, password: m?m.password:'', name:real_name, spec_codes: spec_code.join('\r') })
		this.modal.open({formRef: this.formRef})
		this.getModalSelect()
	}
	
	// 生成条码
	generateCode(){
		const {start_code, end_code} = this.formRef.getValue(['start_code','end_code'])
		if(start_code && (start_code.length === 12 || start_code.length === 14) ){
			if(start_code && end_code && +start_code > +end_code ){
				return message.then(f=>f.warning('开始条码必须小于结束条码'))
			}else{
				if(end_code.length === 12 || end_code.length === 14){
					const arr =[]
					for(var i = parseInt(start_code); i <= parseInt(end_code); i++){
						arr.push(i)
					}
					let {spec_codes} = this.formRef.getValue(['spec_codes'])
					let specData = []
					if(spec_codes){
						spec_codes = spec_codes.match(/\d{14}|\d{12}/g)
						spec_codes.forEach(v=>{
							specData.push(parseInt(v))
						})
					}
					spec_codes = Array.from(new Set([...specData, ...arr]))
					this.formRef.setValue({spec_codes: spec_codes.join('\r')})
				}
			}
		}else{
			return message.then(f=>f.warning('开始条码不正确'))
		}
	}
	
	// 解析条码
	getSpec = () => {
		let {spec_codes} = this.formRef.getValue(['spec_codes'])
		// spec_codes = spec_codes.replace(/\s+|，/g,',')
		// spec_codes = spec_codes.split(',')
		spec_codes = spec_codes.match(/\d{14}|\d{12}/g)
		this.submit.spec_codes = Array.from(new Set(spec_codes)).join(',')
	}
	
	render(){
		return FormModal.call(this, {title:'批量项目作废', labelWidth:'s5', width:800, api:'specimen-kind/endBatch',enter:false, 
			onRefresh:()=> this.props.onRefresh(),onOk:this.getSpec
		} )
	}
}

export default Index